﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Main_ServiceInvoice_CreditNoteView : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    DataTable DTMain = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            //**************Set Minimum and maximum date calender Control********
            AccountSettings.setMinMaxDate(form1);
            //********************End **********
           string finyear = Session["finyear"].ToString();
           txtFrom.Text = "01/04/" + finyear;
           txtTo.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
           BindCustomData();
        }
    }
    protected void BindCustomData()
    {
        DataTable DTMain = new DataTable();
        if (txtFrom.Text != string.Empty && txtTo.Text != string.Empty)
        {
            SqlParameter[] sqlpara = {
                                   new SqlParameter("@datefrom",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtFrom.Text))),
                                   new SqlParameter("@dateto",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtTo.Text)))
                               };
            DTMain = DU.getdatausingprocedure("USP_IVC_GetCreditNote_ViewDetail", sqlpara);
        }

        grdDisplay.DataSource = DTMain;
        grdDisplay.DataBind(); if (DTMain.Rows.Count <= 0)
        {
            custPager.Visible = false;
            CommonControls2.Visible = false;
        }
        else
        {
            custPager.Visible = true;
            CommonControls2.Visible = true;
        }
    }

    protected void grdDisplay_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDisplay.PageIndex = e.NewPageIndex;
        BindCustomData();
    }

    protected void grdDisplay_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "editcn")
        {
            GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            int d = row.RowIndex;
            string date =string.Format("{0:dd/MM/yyyy}",Convert.ToDateTime(grdDisplay.Rows[d].Cells[2].Text.ToString()));
            string [] strArgument = e.CommandArgument.ToString().Split(',');
            string creditnoteid = strArgument[0].ToString();
            string tranid = strArgument[1].ToString();
            Response.Redirect("CreditNoteEdit.aspx?Creditnoteid=" + creditnoteid.ToString() + "&Tranid=" + tranid + "&Date="+date+"");
        }
    }
    protected void grdDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ////DataTable DT = (DataTable)grdDisplay.DataSource;
            //string PoId = grdDisplay.DataKeys[e.Row.RowIndex].Value.ToString();
            //strsql = " Select Status from ACC_POMAIN where POID='" + PoId + "'";
            //DataTable DT = DU.getdata(strsql);

            //if (DT.Rows.Count > 0)
            //{
            //        if (DT.Rows[0]["Status"].ToString() == "2")
            //        {
            //            e.Row.Cells[6].FindControl("lnkEdit").Visible = false;
            //        }
            //        else
            //        {
            //            e.Row.Cells[6].FindControl("imgLock").Visible = false;
            //        }            
            //}
        }
    }
    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        grdDisplay.PageSize = e.CurrentPageSize;
        grdDisplay.PageIndex = e.CurrentPageNumber;
        BindCustomData();
    }
    protected void grdDisplay_RowCreated(object sender, GridViewRowEventArgs e)
    
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtFrom.Text != string.Empty && txtTo.Text != string.Empty)
        {
            BindCustomData();
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
}
 